package com.platform.lxh.netty.resolve;

import com.platform.lxh.netty.model.Message;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @create: 2021-03-10 20:08
 * @author: xiaoh.luo001@bkjk.com
 * @description:
 **/
public final class MessageResolverFactory {

    private static final List<Resolver> resolvers = new CopyOnWriteArrayList<>();

    public MessageResolverFactory() {
    }

    public void registerResolver(Resolver resolver) {
        resolvers.add(resolver);
    }

    // 根据解码后的消息，在工厂类处理器中查找可以处理当前消息的处理器
    public Resolver getMessageResolver(Message message) {
        for (Resolver resolver : resolvers) {
            if (resolver.support(message)) {
                return resolver;
            }
        }

        throw new RuntimeException("cannot find resolver, message type: " + message.getMessageType());
    }

}
